package projectcvip;

import java.rmi.RMISecurityManager;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.entry.Entry;
import net.jini.core.lease.Lease;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.core.transaction.server.TransactionManager;
import net.jini.lookup.entry.Name;
import net.jini.space.JavaSpace;

public abstract class JSBasic implements Runnable
{

    protected long defaultLease = Lease.FOREVER;
    protected JavaSpace space;
    protected TransactionManager txnManager;

    public JSBasic()
    {
    }

    public boolean start(String url/*, String spaceName*/)
    {
        try
        {
            //plik policy.all musi sie znajdowac w glownym katalogu projektu
            System.setProperty("java.security.policy", "policy.all");
            if (System.getSecurityManager() == null)
            {
                //jesli nie ma RMI, to go uruchom
                System.setSecurityManager(new RMISecurityManager());
            }

            //Name sn = new Name(spaceName);
            Class[] txnClasses = new Class[]
            {
                TransactionManager.class
            };
            ServiceTemplate txnTmpl = new ServiceTemplate(null, txnClasses, new Entry[]
                    {
                        /*sn*/
                    });

            LookupLocator locator = new LookupLocator(url);
            ServiceRegistrar sr = locator.getRegistrar();
            txnManager = (TransactionManager) sr.lookup(txnTmpl);

            Class[] jsClasses = new Class[]
            {
                net.jini.space.JavaSpace.class
            };

            ServiceTemplate jsTmpl = new ServiceTemplate(null, jsClasses, new Entry[]
                    {
                        /*sn*/
                    });
            space = (JavaSpace) sr.lookup(jsTmpl);

        }
        catch (Exception ex)
        {
            System.out.println(ex);
            return false;
        }

        Thread thread = new Thread(this);
        thread.start();

        return true;
    }
}
